<template>
	<main-container>
		<!-- 搜索 -->
		<el-form :model="state.queryParams" ref="queryRef" :inline="true" v-show="showSearch">
			<el-form-item label="店铺" prop="shopId">
				<select-shop ref="selectShopRef" v-model="state.queryParams.shopId"></select-shop>
			</el-form-item>
			<el-form-item label="模板名称">
				<el-input v-model="state.queryParams.name" clearable></el-input>
			</el-form-item>
			<el-form-item label="是否包邮">
				<el-select v-model="state.queryParams.isInclPostage" clearable>
					<el-option label="买家承担运费" value="0"></el-option>
					<el-option label="包邮" value="1"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="initPage"
					><el-icon> <ele-Search /> </el-icon>搜索</el-button
				>
				<el-button @click="resetQuery"
					><el-icon> <ele-Refresh /> </el-icon>重置</el-button
				>
			</el-form-item>
		</el-form>
		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div>
				<el-button type="primary" v-auth="'product:freighttemplate:add'" @click="add"
					><el-icon> <ele-Plus /></el-icon>新增</el-button
				>
			</div>
			<right-toolbar :searchBtn="true" :refreshBtn="true" @search="showSearch = !showSearch" @refresh="initPage"></right-toolbar>
		</div>
		<Form ref="formRef" @initPage="initPage"></Form>
		<!-- 列表 -->
		<el-table v-loading="loading" :data="state.tableData" border>
			<el-table-column prop="shopName" label="店铺" width="200" />
			<el-table-column prop="name" label="模板名称" width="200" />
			<el-table-column prop="address" label="发货地址" />
			<el-table-column prop="isInclPostage" label="是否包邮">
				<template #default="scope">
					<el-tag v-if="scope.row.isInclPostage == '0'" type="danger">买家承担运费</el-tag>
					<el-tag v-if="scope.row.isInclPostage == '1'" type="success">包邮</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" />
			<el-table-column label="操作" align="center" width="200">
				<template #default="scope">
					<el-button link type="primary" v-auth="'product:freighttemplate:edit'" @click="edit(scope.row)"
						><el-icon> <ele-Edit /></el-icon>修改</el-button
					>
					<el-button link type="danger" v-auth="'product:freighttemplate:del'" @click="del(scope.row.id)"
						><el-icon> <ele-Delete /></el-icon>删除</el-button
					>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页 -->
		<pagination
			v-model:total="state.page.total"
			v-model:currentPage="state.page.currentPage"
			v-model:pageSize="state.page.pageSize"
			@changePage="initPage"
		></pagination>
	</main-container>
</template>
<script lang="ts" setup>
import { defineAsyncComponent, ref, reactive } from 'vue';
import { getPage, delObj } from '/@/api/product/freighttemplate';
import { ElMessageBox, ElMessage } from 'element-plus';
const Form = defineAsyncComponent(() => import('./form.vue'));
const SelectShop = defineAsyncComponent(() => import('/@/components/selectShop/index.vue'));
const state = reactive({
	queryParams: {
		name: '',
		isInclPostage: '',
		shopId: '',
	},
	page: {
		total: 0,
		currentPage: 1,
		pageSize: 10,
		ascs: [],
		descs: 'create_time',
	},
	tableData: [],
});
const showSearch = ref(true);
const loading = ref(false);
const formRef = ref();

const initPage = async () => {
	loading.value = true;
	let params = {
		current: state.page.currentPage,
		size: state.page.pageSize,
		ascs: state.page.ascs,
		descs: state.page.descs,
	};
	await getPage(Object.assign(params, state.queryParams))
		.then((response: any) => {
			state.tableData = response.data.data.records;
			state.page.total = response.data.data.total;
			loading.value = false;
		})
		.catch(() => {
			loading.value = false;
		});
};
/**
 * 新增按钮
 */
const add = () => {
	formRef.value.initForm();
};
/**
 * 修改按钮
 */
const edit = (row: any) => {
	formRef.value.initForm(row);
};
/**
 * 删除按钮
 */
const del = (id: string) => {
	ElMessageBox.confirm('此操作将删除该运费模板，是否继续?', '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		delObj(id)
			.then(() => {
				ElMessage.success('删除成功');
				initPage();
			})
			.catch(() => {});
	});
};

/**
 * 重置搜索表单
 */
const resetQuery = () => {
	state.queryParams = {
		name: '',
		isInclPostage: '',
		shopId: '',
	};
};
initPage();
</script>
